package com.ruoyi.system.utils;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.Base64;
import niftijio.*;

public class niitobase64 {

    public static void main(String[] args) {

    }

    private static BufferedImage makeimage(String kind,int xyz,NiftiVolume volume){

        if(kind.equals("z")){
            BufferedImage image = new BufferedImage(volume.data.sizeX(), volume.data.sizeY(), BufferedImage.TYPE_BYTE_GRAY);

            // 获取 Graphics2D 对象
            Graphics g = image.getGraphics();

            for (int i=0;i<volume.data.sizeX();i++){
                for (int j=0;j<volume.data.sizeY();j++){
                    Double gray = volume.data.get(volume.data.sizeX()-i-1,volume.data.sizeY()-j-1,xyz,volume.data.dimension()-1);
                    int grayValue = (int) Math.round(gray*0.2);
                    g.setColor(new Color(grayValue, grayValue, grayValue)); // 使用灰度值创建颜色
                    g.fillRect(i, j, 1, 1); // 在指定位置绘制一个像素大小的矩形
                }
            }
            g.dispose();
            return image;
        }

        if(kind.equals("y")){
            BufferedImage image = new BufferedImage(volume.data.sizeX(), volume.data.sizeZ(), BufferedImage.TYPE_BYTE_GRAY);

            // 获取 Graphics2D 对象
            Graphics g = image.getGraphics();

            for (int i=0;i<volume.data.sizeX();i++){
                for (int j=0;j<volume.data.sizeZ();j++){
                    Double gray = volume.data.get(volume.data.sizeX()-i-1,xyz,volume.data.sizeZ()-j-1,volume.data.dimension()-1);
                    int grayValue = (int) Math.round(gray*0.2);
                    g.setColor(new Color(grayValue, grayValue, grayValue)); // 使用灰度值创建颜色
                    g.fillRect(i, j, 1, 1); // 在指定位置绘制一个像素大小的矩形
                }
            }
            g.dispose();
            return image;
        }

        if(kind.equals("x")){
            BufferedImage image = new BufferedImage(volume.data.sizeY(), volume.data.sizeZ(), BufferedImage.TYPE_BYTE_GRAY);

            // 获取 Graphics2D 对象
            Graphics g = image.getGraphics();

            for (int i=0;i<volume.data.sizeY();i++){
                for (int j=0;j<volume.data.sizeZ();j++){
                    Double gray = volume.data.get(xyz,volume.data.sizeY()-i-1,volume.data.sizeZ()-j-1,volume.data.dimension()-1);
                    int grayValue = (int) Math.round(gray*0.2);
                    g.setColor(new Color(grayValue, grayValue, grayValue)); // 使用灰度值创建颜色
                    g.fillRect(i, j, 1, 1); // 在指定位置绘制一个像素大小的矩形
                }
            }
            g.dispose();
            return image;
        }
        return null;
    } //加载nii文件

    // Convert BufferedImage to Base64 string
    private static String imageToBase64(BufferedImage image) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", outputStream);
            byte[] imageBytes = outputStream.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }  //转base64编码

    public static String[] getbase64(String filepath,int x,int y,int z){
        try {
            NiftiVolume volume = NiftiVolume.read(filepath);

            String base64x = "";
            String base64y = "";
            String base64z = "";
            base64x = imageToBase64(makeimage("x",x,volume));
            base64y = imageToBase64(makeimage("y",y,volume));
            base64z = imageToBase64(makeimage("z",z,volume));
            String[] result = new String[3];
            result[0] = base64x;
            result[1] = base64y;
            result[2] = base64z;
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int[] getbase64len(String filepath){
        try {
            NiftiVolume volume = NiftiVolume.read(filepath);

            int[] result = new int[3];
            result[0] = volume.data.sizeX();
            result[1] = volume.data.sizeY();
            result[2] = volume.data.sizeZ();
            return result;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
